﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TJ_MainNameSpace
{
    public partial class For_shujuku : Form
    {
        public For_shujuku()
        {
            InitializeComponent();
        }

        //MySqlConnection con = new MySqlConnection(new MySQLConnectionString(TongJi_EMS.Properties.Settings.Default["server"].ToString(), TongJi_EMS.Properties.Settings.Default["database"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqluser"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqlpassword"].ToString(), shujuyuan3.port).AsString);
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);
        private System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        private OpenFileDialog openfile = new OpenFileDialog();
        public static bool ifalart = false;
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 模拟量点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AnalogPoint a = new AnalogPoint();
            a.Show();

        }

        private void 数字量点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DigitalPoint b = new DigitalPoint();
            b.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            switch (e.Node.Text)
            {
                case "模拟I/O点":
                    AnalogPoint a = new AnalogPoint();
                    a.Show();
                    break;

                case "数字I/O点":
                    DigitalPoint b = new DigitalPoint();
                    b.Show();
                    break;
                case "测试点":
                    TestPoint c = new TestPoint();
                    c.Show();
                    break;
                default:
                    break;
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
        #region 老代码
        //private void fresh()
        //{
        //    con.Open();
        //    string sql = "select db_name,db_slavername,db_address,db_area ,db_discription,db_type from dbpoint where project='" + FormStart.Gongcheng_Name + "'";
        //    MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);
        //    DataSet ds1 = new DataSet();
        //    ds1.Clear();
        //    oda.Fill(ds1, "dbpoint");
        //    this.dataGridView1.DataSource = ds1.Tables["dbpoint"];
        //    this.comboBox1.DataSource = ds1.Tables["dbpoint"];
        //    this.comboBox1.DisplayMember = "db_name";
        //    this.comboBox1.ValueMember = "db_name";
        //    con.Close();
        //} 
        #endregion

        //数据库重新设计后的最新更改
        private void fresh_new()
        {
            con.Open();
            string sql = "select db_name,db_devicename,db_address,db_areaname ,db_description,db_type from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);
            DataSet ds1 = new DataSet();
            //ds1.Clear();
            oda.Fill(ds1, "dbpoint");
            this.dataGridView1.DataSource = ds1.Tables["dbpoint"];
            this.comboBox1.DataSource = ds1.Tables["dbpoint"];
            this.comboBox1.DisplayMember = "db_name";
            this.comboBox1.ValueMember = "db_name";
            con.Close();
        }

        private void For_shujuku_Activated(object sender, EventArgs e)
        {
            try
            {
                //数据库重新设计后的最新更改
                fresh_new();
                //老代码
                //fresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                this.Close();
            }


        }

        private void buttonalert_Click(object sender, EventArgs e)
        {
            ifalart = true;
            string item = "";
            #region 老代码
            //con.Open();
            //string item = comboBox1.Text;
            //string mysql = "select db_name,db_discription,db_area ,db_type,db_address,db_unit,db_slavername from dbpoint where db_name= '" + item + "'AND project='" + FormStart.Gongcheng_Name + "'";
            //MySqlCommand mInsertCmd = new MySqlCommand(mysql, con);

            //MySqlDataReader reader = mInsertCmd.ExecuteReader();
            //reader.Read();
            //switch (reader.GetValue(3).ToString())
            //{
            //    case "模拟量":
            //        AnalogPoint.dbname = reader.GetValue(0).ToString();
            //        AnalogPoint.dbdescription = reader.GetValue(1).ToString();
            //        AnalogPoint.dbarea = reader.GetValue(2).ToString();
            //        AnalogPoint.dbaddress = reader.GetValue(4).ToString();
            //        AnalogPoint.dbsunit = reader.GetValue(5).ToString();
            //        AnalogPoint.dbslavername = reader.GetValue(6).ToString();
            //        AnalogPoint a = new AnalogPoint();

            //        a.Show();
            //        break;

            //    case "数字量":
            //        DigitalPoint.dbname = reader.GetValue(0).ToString();
            //        DigitalPoint.dbdescription = reader.GetValue(1).ToString();
            //        DigitalPoint.dbarea = reader.GetValue(2).ToString();
            //        DigitalPoint.dbaddress = reader.GetValue(4).ToString();
            //        DigitalPoint.dbslavername = reader.GetValue(6).ToString();
            //        DigitalPoint b = new DigitalPoint();
            //        b.Show();
            //        break;
            //    default:

            //        break;
            //}
            //con.Close(); 
            #endregion


            #region 数据库重新设计后的最新更改

            con.Open();
            try
            {
                item = comboBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("没有点可供修改！");
                return;
            }
            string mysql = "select db_name,db_description,db_areaname ,db_type,db_address,db_unit,db_devicename,db_trans from dbpoint where db_name= '" + item + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            MySqlCommand mInsertCmd = new MySqlCommand(mysql, con);

            MySqlDataReader reader = mInsertCmd.ExecuteReader();
            reader.Read();
            switch (reader.GetValue(3).ToString())
            {
                case "Analog":
                    AnalogPoint.dbname = reader.GetValue(0).ToString();
                    AnalogPoint.dbdescription = reader.GetValue(1).ToString();
                    AnalogPoint.dbarea = reader.GetValue(2).ToString();
                    AnalogPoint.dbaddress = reader.GetValue(4).ToString();
                    AnalogPoint.dbsunit = reader.GetValue(5).ToString();
                    AnalogPoint.dbslavername = reader.GetValue(6).ToString();
                    AnalogPoint.dbtrans = reader.GetValue(7).ToString();
                    AnalogPoint a = new AnalogPoint();
                    ifalart = true;
                    a.Show();
                    break;

                case "Digital":
                    DigitalPoint.dbname = reader.GetValue(0).ToString();
                    DigitalPoint.dbdescription = reader.GetValue(1).ToString();
                    DigitalPoint.dbarea = reader.GetValue(2).ToString();
                    DigitalPoint.dbaddress = reader.GetValue(4).ToString();
                    DigitalPoint.dbslavername = reader.GetValue(6).ToString();
                    DigitalPoint b = new DigitalPoint();
                    ifalart = true;
                    b.Show();
                    break;
                case "Test":
                    TestPoint.dbname = reader.GetValue(0).ToString();
                    TestPoint.dbdescription = reader.GetValue(1).ToString();
                    TestPoint.dbarea = reader.GetValue(2).ToString();
                    TestPoint.dbaddress = reader.GetValue(4).ToString();
                    TestPoint.dbslavername = reader.GetValue(6).ToString();
                    TestPoint c = new TestPoint();
                    ifalart = true;
                    c.Show();
                    break;
                default:

                    break;
            }
            con.Close();
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string item = "";
            con.Open();
            try
            {
                item = this.comboBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("没有点可供删除！");
            }
            string mysql = "delete from dbpoint where db_name= '" + item + "'AND  project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            MySqlCommand mInsertCmd = new MySqlCommand(mysql, con);

            mInsertCmd.ExecuteNonQuery();
            con.Close();
            fresh_new();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            saveFileDialog1.Filter = "XML File|*.xml";
            saveFileDialog1.Title = "Save XML File";
            saveFileDialog1.InitialDirectory = TongJi_EMS._Management.ProjectManager.Instance.CurProjectPath;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fimename1 = saveFileDialog1.FileName;

                DataTable dt = new DataTable();
                dt = (DataTable)dataGridView1.DataSource;

                dt.WriteXml(fimename1);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openfile.Filter = "XML File|*.xml"; ;
            openfile.Title = "Save XML File";
            openfile.InitialDirectory = TongJi_EMS._Management.ProjectManager.Instance.CurProjectPath;
            if (openfile.ShowDialog() == DialogResult.OK)
            {

                string filepath = openfile.FileName;
                DataSet ds = new DataSet();
                ds.ReadXml(filepath);
                this.dataGridView1.DataSource = ds.Tables[0];


                this.comboBox1.DataSource = ds.Tables[0];
                this.comboBox1.DisplayMember = "name";
                //this.comboBox1.ValueMember = "name";
            }

        }

    }
}